#-*- encoding:utf8 -*-

import json,types
from tools import tools
from django.template.loader import Context
from django.db import models as db_models
from base import fields as base_fields

class Column(object):
    def __init__(self,request,field):
        self.request = request
        self.field = field

    def get_template(self):
        path = self.field.model.school_get_model_full_name()
        return tools.Import.get_field_template(path,u'columns/%s.html'%self.field.name,u'base/columns/%s.html'%self.field.__class__.__name__.lower())

    def get_context(self):
        return Context({
            u'context' : self
        })

    @property
    def name(self):
        return self.field.name

    def is_sorting(self):
        default_sorting = self.field.model._meta.ordering
        return True if self.field.name in default_sorting or u'-%s'%self.field.name in default_sorting else False

    def search(self,records):
        return records

    def render(self):
        try:
            return self.get_template().render(self.get_context())
        except Exception,ex:
            return json.dumps({
                u'title' : self.field.verbose_name,
                u'data' : self.field.name,
                u'soring' : self.is_sorting()
            })

    def val_from_obj(self,obj):
        value = getattr(obj,self.field.name)
        if value is None:
            if isinstance(self.field,db_models.ForeignKey):
                value = {u'id':0,u'name':''}
            elif isinstance(self.field,db_models.ManyToManyField):
                value = []
            else:
                value = u""
        elif isinstance(self.field,db_models.ForeignKey):
            value = value.school_item()
        elif isinstance(self.field,db_models.ManyToManyField):
            value = value.school_items()
        else:
            value = unicode(value)

        return value

class VColumn(object):
    def __init__(self,request,name,title,is_sorting=False):
        self.request = request
        self.name = name
        self.is_sorting = is_sorting
        self.title = title

    def get_template(self):
        path = self.field.model.school_get_model_full_name()
        return tools.Import.get_field_template(path,u'columns/%s.html'%self.name,u'')

    def get_context(self):
        return Context({
            u'context' : self
        })

    def search(self,records):
        return records

    def render(self):
        try:
            return self.get_template().render(self.get_context())
        except Exception,ex:
            return json.dumps({
                u'title' : self.title,
                u'data' : self.name,
                u'soring' : self.is_sorting
            })

    def val_from_obj(self,obj):
        value = getattr(obj,self.name,u'')
        if type(value) is types.FunctionType:
            value = value()
        elif isinstance(value,object):
            value = unicode(value)
        return value

class Searcher(object):
    def __init__(self,request,field):
        self.request = request
        self.field = field

    @property
    def name(self):
        return self.field.name


    def get_template(self):
        path = self.field.model.school_get_model_full_name()
        return tools.Import.get_field_template(path,u'searchers/%s.html'%self.field.name,u'base/searchers/%s.html'%self.field.__class__.__name__.lower())

    def get_context(self):
        return Context({
            u'context' : self
        })

    def type(self):
        if isinstance(self.field in base_fields.TreeForeignKey):
            return u"TreeSelect"
        elif isinstance(self.field,(db_models.EmailField,db_models.URLField)):
            return self.field.__class__.__name__
        elif isinstance(self.field,(db_models.CharField,db_models.IntegerField)):
            return u'CharField'
        elif isinstance(self.field,db_models.FloatField):
            return u'NumericField'
        elif isinstance(self.field,db_models.DateField):
            return self.field.__class__.__name__
        elif isinstance(self.field,db_models.ForeignKey):
            return u'Select'

    def render(self):
        try:
            raise
            return self.get_template().render(self.get_context())
        except Exception,ex:
            return json.dumps({
                u'title' : self.field.verbose_name,
                u'data' : self.field.name,
                u'type' : u'CharField'
            })

    def search(self,records):
        return records